﻿using BaseData.API.Write.Applications.Commands;
using BaseData.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace BaseData.API.Write.Controllers
{
    /// <summary>
    /// Api控制器--写
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [AllowAnonymous]
    public class BaseDataController : ControllerBase
    {
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<BaseDataController> logger;
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator mediator;

        public BaseDataController(ILogger<BaseDataController> logger,IMediator mediator)
        {
            this.logger = logger;
            this.mediator = mediator;
        }
        #region 权限
        /// <summary>
        /// 添加权限
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddPermission(AddPermissionCommand request)
        {
            try
            {
                logger.LogInformation("-添加权限操作日志-");
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);

                throw;
            }
        }
        /// <summary>
        /// 修改权限
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdPermission(UpdPermissionCommand request)
        {
            try
            {
                logger.LogInformation("-修改权限操作日志-");
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);

                throw;
            }
        }


        /// <summary>
        /// 删除权限
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DelPermission (int PermissionId)
        {
            try
            {
                logger.LogInformation("-删除权限操作日志-");
                DelPermissionCommand command= new DelPermissionCommand();
                command.PermissionId=PermissionId;
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);

                throw;
            }
        }
        #endregion
        #region 用户
        /// <summary>
        /// 添加用户配置角色
        /// </summary>
        /// <param name="request">命令</param>
        /// <returns>返回添加任务</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddUser(AddUserCommand request)
        {
            try
            {
                logger.LogInformation("-添加用户配置角色操作日志-");
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);

                throw;
            }
        }
        /// <summary>
        /// 修改用户--配置角色
        /// </summary>
        /// <param name="request">命令</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdUser(UpdUserCommand request)
        {
            try
            {
                logger.LogInformation("-修改用户--配置角色操作日志-");
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);

                throw;
            }
        }
        /// <summary>
        /// 添加用户配置功能
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddUserByEnum(AddUserByEnumCommand request)
        {
            try
            {
                logger.LogInformation("-添加用户配置功能操作日志-");
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);

                throw;
            }
        }
        /// <summary>
        /// 修改用户配置功能
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdUserByEnum(UpdUserByMenuCommand request)
        {
            try
            {
                logger.LogInformation("-修改用户配置功能操作日志-");
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);

                throw;
            }
        }
        #endregion
        #region 角色
        /// <summary>
        /// 添加角色--配置权限
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddRole(AddRoleCommand request)
        {
            try
            {
                logger.LogInformation("-添加角色--配置权限操作日志-");
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);

                throw;
            }
        }
        /// <summary>
        /// 修改角色--配置权限
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdRole(UpdRoleCommand request)
        {
            try
            {
                logger.LogInformation("-修改角色--配置权限操作日志-");
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);

                throw;
            }
        }
        #endregion
    }
}
